﻿/// <reference path="../../js/utils.js" />
'use strict';

define([
    'backbone',
    'screenView',

    'common/models/truck.model',
    'common/models/travel.model',
    'common/models/travels.coll',
    'common/views/truckDetails/truckDetails.view',

    'backgrid',

    'typeahead',
],
function (Backbone, ScreenView, TruckModel, TravelModel, TravelsColl, TruckDetailsView, Backgrid) {
    var View = ScreenView.extend({
        super: null,

        name: 'truck',
        title: 'Camiones',

        initialize: function () {
            this.super = View.__super__;

            return this.super.initialize.apply(this, arguments);
        },

        render: function () {
            this.super.render.apply(this);

            /* First render actions. */
            if (!this.rendered) {
                this.truckDetails = new TruckDetailsView({
                    el: this.$el.find('#details'),
                    /* Pass a clone to the view model to prevent references. */
                    model: new TruckModel(),
                });
                this.truckDetails.render();


                var travelsColl = new TravelsColl();

                this.travelsGrid = App.utils.grids.travels.truck(travelsColl);
                this.travelsGrid.render();
                // Render the grid and attach the Grid's root to your HTML document
                this.$el.find('#travels').append(this.travelsGrid.el);

                travelsColl.set([
                    new TravelModel({ end: null, }),
                    new TravelModel(),
                    new TravelModel(),
                    new TravelModel(),
                    new TravelModel(),
                ]);


                var eventModel = Backbone.Model.extend({
                    defaults: {
                        description: 'Seguro',
                        nextDatetime: '2015-06-07',
                        owner: 'Nicolas Lareu',
                        previousDatetime: '2015-02-24',
                        reminderDatetime: '2015-05-07',
                        repeatFreq: 'Anual',
                        status: 0,
                    },
                });

                var eventsColl = Backbone.Collection.extend({
                    defaults: {
                        model: eventModel,
                    }
                });

                var col = new eventsColl();

                this.truckEventsGrid = App.utils.grids.events.truck(col);
                this.truckEventsGrid.render();
                // Render the grid and attach the Grid's root to your HTML document
                this.$el.find('#events').append(this.truckEventsGrid.el);                

                col.set([
                    new eventModel({ description: 'SENASA', status: 1, }),
                    new eventModel({ repeatFreq: 'Mensual', }),
                    new eventModel({ status: 2, }),
                    new eventModel({ description: 'SENASA', repeatFreq: 'Cada 3 meses', }),
                    new eventModel(),
                ]);




                this.$el.find('#searchTruckTbx').typeahead({
                    source: ['AYU-132', 'TRY-465', 'WVY-581', 'POT-257', 'ATK-254'],
                });




                /* Indicates that the view was rendered at least one time. */
                this.rendered = true;
            }
            /* Not first render actions. */
            else {
                
            }


            return this;
        },
    });

    return View;
});